﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace devtm.AutoMapper
{


    public class MapperContainers : TreeViewItem
    {


        internal global::System.IServiceProvider ServiceProvider;

        public MapperContainers(global::System.IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
        }


        //private MappingModel root;
        HashSet<TypeElement> lst = new HashSet<TypeElement>();
        //ObservableCollection<MapperContainer> Liste = new ObservableCollection<MapperContainer>();

        public void Refresh(MappingModel root)
        {

            //if (root == this.root)
            //    return;


            this.Items.Clear();
            lst.Clear();


            // On copie la liste principale
            HashSet<TypeElement> lst1 = new HashSet<TypeElement>(lst);


            List<TypeElement> lstTypes = new List<TypeElement>();
            foreach (Mapper mapper in root.Mappers)
                lstTypes.Add(mapper.MappingSource.TypeElement);

            foreach (TypeElement source in lstTypes.OrderBy(c => c.FullName))
            {

                // On supprime ceux qui sont là.
                if (lst1.Contains(source))                
                    lst.Remove(source);
                
                Add(source);

            }
            
            // Il reste les absents.
            foreach (TypeElement item in lst1)                       
                    Remove(item);                                   
            
        }



        private void Add(TypeElement source)
        {

            if (!lst.Contains(source))
            {

                lst.Add(source);

                MapperContainer c = null; //GetItem(source);

                //if (c == null)
                //{

                    string[] nn1 = source.Namespace.Split('.');

                    int index = 0;
                    MapperContainer parent = GetNamespace(nn1, ref index, source);

                    if (parent == null)
                    {
                        c = new MapperContainer(nn1, 0, source);
                        AddChild(c);
                    }
                    else
                    {
                        index++;
                        parent.Insert(nn1, index, source);
                    }
                //}
                //else
                //{
                //    //c.Insert(source);
                //}
                
               

            }

        }

        private MapperContainer GetNamespace(string[] nn1, ref int index, TypeElement source)
        {
            string n = nn1[index];
            MapperContainer retour = null;

            foreach (MapperContainer item in this.Items)
                if ((item.Name as string) == n)
                    if (index + 1 < nn1.Length)
                    {
                        index++;
                        retour = item.GetNamespace(nn1, ref index, source);
                        
                        if (retour != null)
                            break;

                    }                    

            return retour;

        }

        //private MapperContainer GetItem(TypeElement source)
        //{

        //    string Namespace = source.Namespace;

        //    foreach (MapperContainer item in this.Items)
        //    {

        //        MapperContainer i = item.GetItem(source);
        //        if (i != null)
        //            return i;

        //    }


        //    return null;
        //}

        private void Remove(TypeElement source)
        {

            if (lst.Contains(source))
            {

                lst.Remove(source);

            }

        }


    }



}
